<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索 - 校园表白墙</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
    <nav class="navbar navbar-light bg-white shadow-sm">
        <div class="container">
            <button class="btn btn-link" onclick="history.back()">
                <i class="bi bi-arrow-left"></i>
            </button>
            <div class="flex-fill mx-3">
                <div class="input-group">
                    <input type="text" class="form-control" id="searchInput" placeholder="搜索..." value="">
                    <button class="btn btn-primary" onclick="search()">
                        <i class="bi bi-search"></i>
                    </button>
                </div>
            </div>
        </div>
    </nav>
    
    <div class="container mt-3">
        <div id="searchResults"></div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        function init() {
            if (!checkLogin()) return;
            
            const params = new URLSearchParams(window.location.search);
            const keyword = params.get('keyword');
            
            if (keyword) {
                $('#searchInput').val(keyword);
                performSearch(keyword);
            }
        }
        
        function search() {
            const keyword = $('#searchInput').val().trim();
            if (!keyword) {
                showToast('请输入搜索关键词', 'warning');
                return;
            }
            performSearch(keyword);
        }
        
        function performSearch(keyword) {
            showLoading('#searchResults');
            
            request(`/posts/search?page=1&size=50&keyword=${encodeURIComponent(keyword)}`)
                .then(data => {
                    if (data.records && data.records.length > 0) {
                        $('#searchResults').empty();
                        data.records.forEach(post => {
                            $('#searchResults').append(renderPostCard(post));
                        });
                    } else {
                        showEmpty('#searchResults', '没有找到相关内容');
                    }
                })
                .catch(err => {
                    showToast(err.message, 'danger');
                });
        }
        
        $('#searchInput').keypress(function(e) {
            if (e.which === 13) {
                search();
            }
        });
        
        $(document).ready(init);
    </script>
</body>
</html>

